﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;
using BLL;
namespace stock.GuPiao
{
    public partial class Regist : System.Web.UI.Page
    {
        CustomerBLL BL = new CustomerBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] != null)
            {
                Response.Redirect("user.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid==true)
            {
                String script = "";
                if (Session["check"] == null)
                {
                    script = "<script language='Javascript'>alert('页面已过期！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    return;
                }
                if (code.Value.ToLower().Trim() != Session["check"].ToString().ToLower())
                {
                    script = "<script language='Javascript'>alert('验证码错误！"+Session["check"].ToString()+"');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    return;
                }
                String tbName = name.Value.Trim();
                String tbPwd = password.Value.Trim();
                String tbRePwd = rePassword.Value.Trim();
                String tbNick = nick.Value.Trim();
                String tbEmail = mail.Value.Trim();
                String tbPhone = cellphone.Value.Trim();
                if (BL.Exists(tbName))
                {
                    script = "<script language='Javascript'>alert('用户名已经存在！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    return;
                }
                if (tbPwd != tbRePwd)
                {
                    script = "<script language='Javascript'>alert('两次密码不相同！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    return;
                }
                Customer userModel = new Customer();
                userModel.CreateTime = DateTime.Now;
                userModel.CellPhone = tbPhone;
                userModel.Nick = tbNick;
                userModel.name = tbName;
                userModel.Email = tbEmail;
                userModel.Password =DESEncrypt.Encrypt( tbPwd);
                userModel.Status = 0;
                int back = BL.Add(userModel);
                if (back <= 0)
                {
                    script = "<script language='Javascript'>alert('注册失败，请重试！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    return;
                }
                Session["id"] = back;//设置登陆的id
                script = "<script language='Javascript'>alert('注册成功！');window.location=stock.aspx;</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                return;
            }
        }
    }
}